package eu.vranckaert.workRegistrationWidget.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.google.inject.Inject;
import eu.vranckaert.workRegistrationWidget.R;
import eu.vranckaert.workRegistrationWidget.domain.TimeRegistration;
import eu.vranckaert.workRegistrationWidget.service.RegistrationService;
import eu.vranckaert.workRegistrationWidget.service.WidgetLayoutBuilderService;
import eu.vranckaert.workRegistrationWidget.util.WidgetUpdater;
import roboguice.activity.GuiceActivity;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * @author Dirk Vranckaert
 *         Date: 7-jun-2010
 *         Time: 19:28:08
 */
public class CreateCustomRegistrationActivity extends GuiceActivity {
    private static final String LOG_TAG = CreateCustomRegistrationActivity.class.getName();

    private static final int DIALOG_ENDDATE_BEFORE_STARTDATE = 0;

    @Inject
    RegistrationService registrationService;

    @Inject
    WidgetLayoutBuilderService widgetLayoutBuilderService;

    private TimeRegistration registration = new TimeRegistration();

    private int step = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        updateViewForStep();
    }

    private void updateViewForStep() {
        switch(step) {
            case 0:
                setContentView(R.layout.registration_edit_step1);

                TextView subTitle1 = (TextView) findViewById(R.id.regEditSubTitleStep1);
                subTitle1.setText(R.string.regAddSubTitleStep1);

                final Calendar startCalendar = new GregorianCalendar();
                final DatePicker startDatePicker = (DatePicker) findViewById(R.id.regEditStartDatePicker);
                final TimePicker startTimePicker = (TimePicker) findViewById(R.id.regEditStartTimePicker);
                startTimePicker.setIs24HourView(true);

                Button nextButtonStep1 = (Button) findViewById(R.id.regEditNext);
                Button cancelButtonStep1 = (Button) findViewById(R.id.regEditCancel);

                nextButtonStep1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d(LOG_TAG, "Next button clicked from step " + step);

                        int year = startDatePicker.getYear();
                        int month = startDatePicker.getMonth();
                        int day = startDatePicker.getDayOfMonth();
                        int hour = startTimePicker.getCurrentHour();
                        int minute = startTimePicker.getCurrentMinute();

                        startCalendar.set(Calendar.YEAR, year);
                        startCalendar.set(Calendar.MONTH, month);
                        startCalendar.set(Calendar.DAY_OF_MONTH, day);
                        startCalendar.set(Calendar.HOUR_OF_DAY, hour);
                        startCalendar.set(Calendar.MINUTE, minute);

                        registration.setStartDate(startCalendar.getTime());

                        goToNextStep();
                    }
                });

                cancelButtonStep1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish(false);
                    }
                });

                break;
            case 1:
                setContentView(R.layout.registration_edit_step2);

                TextView subTitle2 = (TextView) findViewById(R.id.regEditSubTitleStep2);
                subTitle2.setText(R.string.regAddSubTitleStep2);

                final Calendar stopCalendar = new GregorianCalendar();

                final DatePicker stopDatePicker = (DatePicker) findViewById(R.id.regEditStopDatePicker);
                final TimePicker stopTimePicker = (TimePicker) findViewById(R.id.regEditStopTimePicker);
                final CheckBox regNotYetEnded = (CheckBox) findViewById(R.id.regEditStopNotYetEnded);
                stopTimePicker.setIs24HourView(true);

                /*
                 *  - is laatste registratie lopende registratie
                 *     - zo ja, disable regNotYetEnded EN is startdatum registratie voor laatste registratie
                 *  - zo nee, alles mogelijk
                 */

                TimeRegistration latestRegistration = registrationService.getLatestTimeRegistration(this);
                if(latestRegistration.getEndDate() == null || latestRegistration.getEndDate().getTime() == 0L) {
                    regNotYetEnded.setEnabled(false);
                }

                regNotYetEnded.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
                        if(checked) {
                            stopDatePicker.setEnabled(false);
                            stopTimePicker.setEnabled(false);
                        } else {
                            stopDatePicker.setEnabled(true);
                            stopTimePicker.setEnabled(true);
                        }
                    }
                });

                Button nextButtonStep2 = (Button) findViewById(R.id.regEditNext);
                Button cancelButtonStep2 = (Button) findViewById(R.id.regEditCancel);

                nextButtonStep2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d(LOG_TAG, "Next button clicked from step " + step);

                        if (regNotYetEnded.isChecked()) {
                            registration.setEndDate(null);
                        } else {
                            int year = stopDatePicker.getYear();
                            int month = stopDatePicker.getMonth();
                            int day = stopDatePicker.getDayOfMonth();
                            int hour = stopTimePicker.getCurrentHour();
                            int minute = stopTimePicker.getCurrentMinute();

                            stopCalendar.set(Calendar.YEAR, year);
                            stopCalendar.set(Calendar.MONTH, month);
                            stopCalendar.set(Calendar.DAY_OF_MONTH, day);
                            stopCalendar.set(Calendar.HOUR_OF_DAY, hour);
                            stopCalendar.set(Calendar.MINUTE, minute);

                            registration.setEndDate(stopCalendar.getTime());
                            
                            if (registration.getStartDate().compareTo(registration.getEndDate()) <= 0) {
                                goToNextStep();
                            } else {
                                showDialog(DIALOG_ENDDATE_BEFORE_STARTDATE);
                            }
                        }
                    }
                });

                cancelButtonStep2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish(false);
                    }
                });

                break;
            case 2:
                setContentView(R.layout.registration_edit_step3);

                TextView subTitle3 = (TextView) findViewById(R.id.regEditSubTitleStep3);
                subTitle3.setText(R.string.regAddSubTitleStep3);

                Button finishButton = (Button) findViewById(R.id.regEditFinish);
                Button cancelButtonStep3 = (Button) findViewById(R.id.regEditCancel);

                finishButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish(true);
                    }
                });

                cancelButtonStep3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish(false);
                    }
                });
        }
    }

    private void goToNextStep() {
        step++;
        updateViewForStep();
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;

        switch (id) {
            case DIALOG_ENDDATE_BEFORE_STARTDATE:
                AlertDialog.Builder alertBuilder2 = new AlertDialog.Builder(this);
                alertBuilder2.setTitle(R.string.warning)
                           .setMessage(R.string.regEdit_exc_enddate_before_startdate)
                           .setCancelable(false)
                           .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.cancel();
                                }
                            });
                AlertDialog alertDialog2 = alertBuilder2.create();
                dialog = alertDialog2;
                break;
        }

        return dialog;
    }

    private void finish(boolean completed) {
        if (completed) {
            registrationService.saveOrUpdateRegistration(this, registration);
            updateWidget();
            setResult(RESULT_OK);
        } else {
            setResult(RESULT_CANCELED);
        }
        super.finish();
    }

    private void updateWidget() {
        TimeRegistration registration = registrationService.getLatestTimeRegistration(this);
        WidgetUpdater.updateWidget(this, registration, widgetLayoutBuilderService);
    }
}
